import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/Layout.vue'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import CaregiverDashboard from '@/views/CaregiverDashboard.vue'
import Elder from '@/views/Elder.vue'
import Meal from '@/views/Meal.vue'
import Accident from '@/views/Accident.vue'
import Checkin from '@/views/Checkin.vue'
import Profile from '@/views/Profile.vue'
import Medicine from '@/views/Medicine.vue'
import MedicineReceive from "@/views/MedicineReceive.vue";
import Medical from '@/views/Medical.vue'
import Timeline from '@/views/Timeline.vue'
import Family from '@/views/Family.vue'
import Outing from '@/views/Outing.vue'
import OutingReturn from '@/views/OutingReturn.vue'
import User from '@/views/User.vue'
import Welcome from '@/views/Welcome.vue'
import Messages from '@/views/Messages.vue'
import CaregiverPhotoUpload from '@/views/caregiver/CaregiverPhotoUpload.vue'
import FamilyPhotos from '@/views/family/FamilyPhotos.vue'
import ConsultDetail from '@/views/ConsultDetail.vue'
import HealthAssessmentDetail from '@/views/HealthAssessmentDetail.vue'
import AgreementSigning from '@/views/AgreementSigning.vue'
import SafeCheckinDetail from '@/views/SafeCheckinDetail.vue'


const routes = [
    { 
        path: '/', 
        redirect: '/welcome'
    },
    { path: '/welcome', component: Welcome },
    { path: '/login', component: Login },
    { path: '/register', component: Register },
    { path: '/consult-detail', component: ConsultDetail, name: 'consult-detail' },
    { path: '/health-assessment-detail', component: HealthAssessmentDetail, name: 'health-assessment-detail' },
    { path: '/agreement-signing', component: AgreementSigning, name: 'agreement-signing' },
    { path: '/safe-checkin-detail', component: SafeCheckinDetail, name: 'safe-checkin-detail' },
    {
        path: '/dashboard',
        component: Layout,
        redirect: (to) => {
            // 根据用户角色动态重定向
            const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
            const role = userInfo.role || 'caregiver'
            
            const roleRoutes = {
                'caregiver': '/dashboard/caregiver-dashboard',
                'family': '/dashboard/family-dashboard',
                'admin': '/dashboard/admin-dashboard',
                'reception': '/dashboard/reception-dashboard'
            }
            
            return roleRoutes[role] || '/dashboard/caregiver-dashboard'
        },
        children: [
            // 仪表板路由
            { 
                path: 'caregiver-dashboard', 
                component: CaregiverDashboard,
                name: 'caregiver-dashboard'
            },
            { 
                path: 'family-dashboard', 
                component: () => import('../views/FamilyDashboard.vue'),
                name: 'family-dashboard'
            },
            { 
                path: 'admin-dashboard', 
                component: () => import('../views/AdminDashboard.vue'),
                name: 'admin-dashboard'
            },
            { 
                path: 'reception-dashboard', 
                component: () => import('../views/ReceptionDashboard.vue'),
                name: 'reception-dashboard'
            },
            
            // 护工专属路由
            { path: 'caregiver/elder', component: () => import('../views/caregiver/CaregiverElder.vue'), name: 'caregiver-elder' },
            { path: 'caregiver/meal', component: () => import('../views/caregiver/CaregiverMeal.vue'), name: 'caregiver-meal' },
            { path: 'caregiver/accident', component: () => import('../views/caregiver/CaregiverAccident.vue'), name: 'caregiver-accident' },
            { path: 'caregiver/medicine-receive', component: () => import('../views/caregiver/CaregiverMedicineReceive.vue'), name: 'caregiver-medicine-receive' },
            { path: 'caregiver/medical', component: () => import('../views/caregiver/CaregiverMedical.vue'), name: 'caregiver-medical' },
            { path: 'caregiver/messages', component: () => import('../views/caregiver/CaregiverMessages.vue'), name: 'caregiver-messages' },
            { path: 'caregiver/outing', component: () => import('../views/caregiver/CaregiverOuting.vue'), name: 'caregiver-outing' },
            { path: 'caregiver/profile', component: Profile, name: 'caregiver-profile' },
            { path: 'caregiver/photo-upload', component: CaregiverPhotoUpload, name: 'caregiver-photo-upload' },
            
            // 家属专属路由
            { path: 'family/elder', component: () => import('../views/family/FamilyElder.vue'), name: 'family-elder' },
            { path: 'family/meal', component: () => import('../views/family/FamilyMeal.vue'), name: 'family-meal' },
            { path: 'family/accident', component: () => import('../views/family/FamilyAccident.vue'), name: 'family-accident' },
            { path: 'family/medical', component: () => import('../views/family/FamilyMedical.vue'), name: 'family-medical' },
            { path: 'family/messages', component: () => import('../views/family/FamilyMessages.vue'), name: 'family-messages' },
            { path: 'family/outing', component: () => import('../views/family/FamilyOuting.vue'), name: 'family-outing' },
            { path: 'family/outing-return', component: () => import('../views/family/FamilyOutingReturn.vue'), name: 'family-outing-return' },
            { path: 'family/profile', component: Profile, name: 'family-profile' },
            { path: 'family/photos', component: FamilyPhotos, name: 'family-photos' },
            
            // 管理员专属路由
            { path: 'admin/elder', component: () => import('../views/admin/AdminElder.vue'), name: 'admin-elder' },
            { path: 'admin/meal', component: () => import('../views/admin/AdminMeal.vue'), name: 'admin-meal' },
            { path: 'admin/accident', component: () => import('../views/admin/AdminAccident.vue'), name: 'admin-accident' },
            { path: 'admin/checkin', component: Checkin, name: 'admin-checkin' },
            { path: 'admin/medicine', component: Medicine, name: 'admin-medicine' },
            { path: 'admin/family', component: Family, name: 'admin-family' },
            { path: 'admin/outing', component: Outing, name: 'admin-outing' },
            { path: 'admin/outing-return', component: OutingReturn, name: 'admin-outing-return' },
            { path: 'admin/medical', component: Medical, name: 'admin-medical' },
            { path: 'admin/user', component: User, name: 'admin-user' },
            { path: 'admin/announcement', component: () => import('../views/admin/AdminAnnouncement.vue'), name: 'admin-announcement' },
            { path: 'admin/profile', component: Profile, name: 'admin-profile' },
            
            // 前台专属路由
            { path: 'reception/checkin', component: () => import('../views/reception/ReceptionCheckin.vue'), name: 'reception-checkin' },
            { path: 'reception/outing', component: () => import('../views/reception/ReceptionOuting.vue'), name: 'reception-outing' },
            { path: 'reception/visitor', component: () => import('../views/reception/ReceptionVisitor.vue'), name: 'reception-visitor' },
            { path: 'reception/bed', component: () => import('../views/reception/ReceptionBed.vue'), name: 'reception-bed' },
            { path: 'reception/caregiver', component: () => import('../views/reception/ReceptionCaregiver.vue'), name: 'reception-caregiver' },
            { path: 'reception/messages', component: () => import('../views/reception/ReceptionMessages.vue'), name: 'reception-messages' },
            { path: 'reception/announcement', component: () => import('../views/reception/ReceptionAnnouncement.vue'), name: 'reception-announcement' },
            { path: 'reception/carousel', component: () => import('../views/reception/ReceptionCarousel.vue'), name: 'reception-carousel' },
            { path: 'reception/consult', component: () => import('../views/reception/ReceptionConsult.vue'), name: 'reception-consult' },
            { path: 'reception/profile', component: Profile, name: 'reception-profile' },
            
            // 向后兼容的旧路由（暂时保留）
            { path: 'elder', component: Elder, name: 'elder' },
            { path: 'meal', component: Meal, name: 'meal' },
            { path: 'accident', component: Accident, name: 'accident' },
            { path: 'checkin', component: Checkin, name: 'checkin' },
            { path: 'profile', component: Profile, name: 'profile' },
            { path: 'medicine/receive', component:MedicineReceive, name: 'medicine-receive'},
            { path: 'family', component: Family, name: 'family' },
            { path: 'outing', component: Outing, name: 'outing' },
            { path: 'medicine', component: Medicine, name: 'medicine' },
            { path: 'outing-return', component: OutingReturn, name: 'outing-return' },
            { path: 'user', component: User, name: 'user' },
            { path: 'medical', component: Medical, name: 'medical' },
            { path: 'timeline', component: Timeline, name: 'timeline' },
            { path: 'messages', component: Messages, name: 'messages' }
        ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})


export default router
